//
//  SISSPassengerHouseTakeLookRecordCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/9/7.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSImportantPassengerTakeLookRecordCell.h"
#import "SISSPassengerTakeLookModel.h"
#import "SISSHouseTagsLabelView.h"

@interface SISSImportantPassengerTakeLookRecordCell ()

/// checkButton
@property (weak, nonatomic) UIButton *checkButton;
@property (weak, nonatomic) UIView *tapView;

/// 房屋信息Label
@property (weak, nonatomic) UILabel *infoLabel;
/// 看房时间Label
@property (weak, nonatomic) UILabel *timeTitleLabel;
/// 时间Label
@property (weak, nonatomic) UILabel *timeLabel;
/// 看房记录标签Label
@property (weak, nonatomic) UILabel *tagLabel;
/// 房源标签Label
@property (weak, nonatomic) SISSHouseTagsLabelView *houseTagsLabelView;
/// 底部线条
@property (weak, nonatomic) UIView *lineView;

/// 状态图片
@property (weak, nonatomic) UIImageView *stateImageView;

@end
@implementation SISSImportantPassengerTakeLookRecordCell

- (void)setTakeLookModel:(SISSPassengerTakeLookModel *)takeLookModel {
    _takeLookModel = takeLookModel;
    
    self.infoLabel.text = [NSString stringWithFormat:@"%@ | %@万 \n\n%@-%@ | %@㎡ | %@",takeLookModel.neighbourhood,@(takeLookModel.price),takeLookModel.region,takeLookModel.regionDetail,@(takeLookModel.area),takeLookModel.houseType];
    self.timeLabel.text = [takeLookModel.takeLookDate substringToIndex:10];
    self.checkButton.selected = takeLookModel.isSelected;
    NSString *tagStr;
    UIColor *tagColor;
    if (self.takeLookModel.takelookType == 1) { // 线上
        if (self.takeLookModel.sellerState == 0 && self.takeLookModel.buyerState == 0) {
            tagStr = @"待确认";
            tagColor = COLOR(0xFF1F1D);
            self.checkButton.userInteractionEnabled = NO;
            self.tapView.hidden = YES;
        } else if (self.takeLookModel.sellerState == 2 || self.takeLookModel.buyerState == 2) {
            tagStr = self.takeLookModel.sellerState == 2 ? @"卖家否认看房" : @"买家否认看房";
            tagColor = COLOR(0xFF1F1D);
            self.checkButton.userInteractionEnabled = NO;
            self.tapView.hidden = YES;
        } else if (self.takeLookModel.sellerState == 1 && self.takeLookModel.buyerState == 1) {
            tagStr = @"已确认";
            self.checkButton.userInteractionEnabled = YES;
            self.tapView.hidden = NO;
        } else if (self.takeLookModel.sellerState == 1 || self.takeLookModel.buyerState == 1) {
            tagStr = self.takeLookModel.sellerState == 1 ? @"等待买家确认" : @"等待卖家确认";
            tagColor = COLOR(0xFF1F1D);
            self.checkButton.userInteractionEnabled = NO;
            self.tapView.hidden = YES;
        }
        } else if (self.takeLookModel.takelookType == 2) { // 线下
            switch (takeLookModel.buyerState) {
                case 0:
                    tagStr = @"待确认";
                    tagColor = COLOR(0xFF1F1D);
                    self.checkButton.userInteractionEnabled = NO;
                    self.tapView.hidden = YES;
                    break;
                    
                case 1:
                    tagStr = @"已确认";
                    tagColor = COLOR(0xFF1F1D);
                    self.checkButton.userInteractionEnabled = YES;
                    self.tapView.hidden = NO;
                    break;
                    
                case 2:
                    tagStr = @"已否认";
                    tagColor = COLOR(0xFF1F1D);
                    self.checkButton.userInteractionEnabled = NO;
                    self.tapView.hidden = YES;
                    break;
                    
                default:
                    break;
            }
        }
    
    self.tagLabel.textColor = tagColor;
    self.tagLabel.text = tagStr;
    
    if (takeLookModel.tradeHouse == 1 || takeLookModel.deleted == 1 || takeLookModel.trading == 1) {
        NSString *imageName = @"";
        if (takeLookModel.deleted == 1) {
            imageName = @"已失效";
        }
        if (takeLookModel.trading == 1) {
            imageName = @"签约中";
        }
        if (takeLookModel.tradeHouse == 1) {
            imageName = @"已签约";
        }
        self.stateImageView.image = [UIImage imageNamed:imageName];
        self.infoLabel.textColor = SECONDARY_TEXT_COLOR;
        self.timeTitleLabel.textColor = SECONDARY_TEXT_COLOR;
        self.timeLabel.textColor = SECONDARY_TEXT_COLOR;
        self.tagLabel.textColor = SECONDARY_TEXT_COLOR;
        self.checkButton.userInteractionEnabled = NO;
        self.tapView.hidden = YES;
    }
    
    [self.houseTagsLabelView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (takeLookModel.listTag.count > 0) {
            make.height.mas_equalTo(30);
        } else {
            make.height.mas_equalTo(1);
        }
    }];
    
    
    for (UILabel *label in self.houseTagsLabelView.markLabels) {
        
        label.hidden = YES;
    }
    
    NSArray<UIColor *> *labelCololors = @[COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00)];
    
    for (int i = 0; i < takeLookModel.listTag.count; i++) {
        UILabel *label = self.houseTagsLabelView.markLabels[i];
        if ([takeLookModel.listTag[i].tag isEqualToString:@""]) continue;
        label.hidden = NO;
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@" %@ ",takeLookModel.listTag[i].tag];;
        NSInteger colorIndex = takeLookModel.listTag[i].color - 1;
        label.layer.borderColor = labelCololors[colorIndex].CGColor;
        label.textColor = labelCololors[colorIndex];
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
}

- (void)setIsHiddenCheckButton:(BOOL)isHiddenCheckButton {
    _isHiddenCheckButton = isHiddenCheckButton;
    self.stateImageView.hidden = isHiddenCheckButton;
    self.checkButton.hidden = isHiddenCheckButton;
}

/**
 按钮点击事件
 */
- (void)checkButtonClickAction:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(importantPassengerTakeLookRecordCell:checkButtonIsSelected:)]) {
        BOOL doSelection = [self.delegate importantPassengerTakeLookRecordCell:self checkButtonIsSelected:!self.checkButton.isSelected];
        if(!doSelection){
            
        }else{
            self.takeLookModel.isSelected = !self.takeLookModel.isSelected;
            self.checkButton.selected = self.takeLookModel.isSelected;
        }
    }
}

- (void)tapAction {
    [self checkButtonClickAction:self.checkButton];
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"mainCheckSelected"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(checkButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:button];
    self.checkButton = button;
    
    UILabel *infoLabel = [[UILabel alloc] init];
    infoLabel.textColor = MAIN_TEXT_COLOR;
    infoLabel.font = [UIFont systemFontOfSize:13];
    infoLabel.numberOfLines = 0;
    infoLabel.text = @"小区 | 售价 | 区域 | 面积 | 户型";
    [self.contentView addSubview:infoLabel];
    self.infoLabel = infoLabel;
    
    UILabel *timeTitleLabel = [[UILabel alloc] init];
    timeTitleLabel.textColor = MAIN_TEXT_COLOR;
    timeTitleLabel.font = [UIFont systemFontOfSize:13];
    timeTitleLabel.text = @"看房时间：";
    [self.contentView addSubview:timeTitleLabel];
    self.timeTitleLabel = timeTitleLabel;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.textColor = MAIN_TEXT_COLOR;
    timeLabel.font = [UIFont systemFontOfSize:13];
    timeLabel.text = @"时间";
    [self.contentView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    SISSHouseTagsLabelView *houseTagView = [[SISSHouseTagsLabelView alloc] init];
    [self.contentView addSubview:houseTagView];
    self.houseTagsLabelView = houseTagView;
    
    UILabel *tagLabel = [[UILabel alloc] init];
    tagLabel.textColor = SECONDARY_TEXT_COLOR;
    tagLabel.font = [UIFont systemFontOfSize:13];
    tagLabel.text = @"看房记录标签";
    [self.contentView addSubview:tagLabel];
    self.tagLabel = tagLabel;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:lineView];
    self.lineView = lineView;
    
    UIImageView *stateImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:stateImageView];
    self.stateImageView = stateImageView;
    
    UIView *tapView = [UIView new];
    [self.contentView addSubview:tapView];
    self.tapView = tapView;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [tapView addGestureRecognizer:tap];
}


#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat margin = 8.0;
    WeakSelf
    [self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.contentView);
        make.left.equalTo(weakSelf.contentView).offset(margin);
    }];
    
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(2 * margin);
        make.left.equalTo(weakSelf.checkButton.mas_right).offset(margin);
        make.right.lessThanOrEqualTo(weakSelf.contentView).offset(margin).priority(999);
    }];
    
    [self.timeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.infoLabel);
        make.top.equalTo(weakSelf.infoLabel.mas_bottom).offset(2 * margin);
//        make.bottom.equalTo(weakSelf.lineView).offset(-2 * margin);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeTitleLabel.mas_right);
        make.centerY.equalTo(weakSelf.timeTitleLabel);
    }];
    
    [self.houseTagsLabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeTitleLabel);
        make.top.equalTo(weakSelf.timeTitleLabel.mas_bottom).offset(margin);
        make.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(4 * margin);
        make.bottom.equalTo(weakSelf.lineView).offset(-2 * margin);
    }];
    
    [self.tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.contentView);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
    
    [self.stateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(4 * margin);
        make.centerY.equalTo(weakSelf.contentView);
        make.size.mas_equalTo(CGSizeMake(66, 66));
    }];
    
    [self.tapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(weakSelf.contentView);
        make.right.equalTo(weakSelf.infoLabel.mas_left);
    }];
}


@end
